﻿using System;
using System.Web.Mvc;

using mvcForum.Core.Abstractions.Interfaces;
using mvcForum.Core.Interfaces.AddOns;
using mvcForum.SearchProvider.Lucene.ViewModels;
using mvcForum.Web.Controllers;
using mvcForum.Web.Helpers;
using mvcForum.Web.Interfaces;
using mvcForum.Core.Interfaces.Data;

namespace mvcForum.SearchProvider.Lucene.Controllers {

	public class LuceneConfigurationController : ForumAdminBaseController, ISearchConfigurationController {
		private readonly LuceneConfiguration config;

		public LuceneConfigurationController(IWebUserProvider userProvider, IContext context, IAddOnConfiguration<Indexer> config)
			: base(userProvider, context) {

			this.config = (LuceneConfiguration)config;
		}

		public ViewResult Index() {
			return View(
				new LuceneViewModel {
					Enabled = this.config.Enabled,
					RunAsynchronously = this.config.RunAsynchronously,
					Delay = this.config.Delay,
					AnnouncementWeight = this.config.AnnouncementWeight,
					StickyWeight = this.config.StickyWeight,
					TitleWeight = this.config.TitleWeight,
					TopicWeight = this.config.TopicWeight
				});
		}

		[HttpPost]
		public ViewResult Index(LuceneViewModel model) {
			if (ModelState.IsValid) {
				this.config.Enabled = model.Enabled;
				this.config.Delay = model.Delay;
				this.config.RunAsynchronously = model.RunAsynchronously;
				this.config.AnnouncementWeight = model.AnnouncementWeight;
				this.config.StickyWeight = model.StickyWeight;
				this.config.TitleWeight = model.TitleWeight;
				this.config.TopicWeight = model.TopicWeight;

				this.Context.SaveChanges();

				TempData["Saved"] = true;
			}
			return View(model);
		}

		public String Name {
			get {
				return ForumHelper.GetString<mvcForum.SearchProvider.Lucene.Indexer>("Name");
			}
		}

		public String Description {
			get {
				return ForumHelper.GetString<mvcForum.SearchProvider.Lucene.Indexer>("Description");
			}
		}
	}
}